<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>通讯录</title>
    <style>
        h1{ color:red;}
        input{
            padding: 8px;
            border-radius: 6px;
            margin: 5px 0;
        }
        .errMsg{
            color: red;
            font-weight: bold;
        }
    </style>

</head>
<body><?php
$db=new PDO('mysql:host=localhost;dbname=db','root','12qwas');
if($_POST){ //判断是否提交有数据
    try {
        $xh = $_POST['xh'];
        $name = $_POST['name'];
        $tel = $_POST['tel'];
        $ps = $db->prepare("insert into students(xh,name,tel) values(?,?,?)");
        $r = $ps->execute(array($xh, $name, $tel));
        header("location: index.php");
        return;
    }catch(Throwable $e){
        $errMsg = $e->getMessage(); //获取错误信息
    }
}
?>
<h1>添加新用户</h1>
<form method="post">
    学号：<input type="text" name="xh" value="<?=htmlentities($xh)?>" /><br>
    姓名：<input type="text" name="name" value="<?=htmlentities($name)?>" /><br>
    电话：<input type="text" name="tel" value="<?=htmlentities($tel)?>" /><br>
    <div class="errMsg"><?=$errMsg?></div>
    学号：<input type="text" name="xh" value="<?=htmlentities($xh??'')?>" /><br>
    姓名：<input type="text" name="name" value="<?=htmlentities($name??'')?>" /><br>
    电话：<input type="text" name="tel" value="<?=htmlentities($tel??'')?>" /><br>
    <div class="errMsg"><?=htmlentities($errMsg??'')?></div>
    <input type="submit" value=" 创建新用户 " /><br>
</form>

</body>
</html>